package com.svn.ui.control;

import java.awt.event.ActionEvent;

import javax.swing.JComponent;
import javax.swing.JTextPane;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.AbstractDocument;
import javax.swing.text.JTextComponent;
import javax.swing.undo.UndoManager;

import conf.MenuActionCommandEnum;

public class EditMenuAction extends BasAction {

    private JTextComponent text;
    private AbstractDocument document;
    private UndoableEditListener[] undoableEditListeners;

    @Override
    public void actionPerformed(ActionEvent arg0) {
	switch (MenuActionCommandEnum.valueOf(arg0.getActionCommand())) {
	case COPYITEM:
	    text.copy();
	    break;
	case CUTITEM:
	    text.cut();
	    break;
	case PASTEITEM:
	    text.paste();
	    break;
	case SELECTALLITEM:
	    text.selectAll();
	    break;
	case DELETEITEM:

	    break;
	case REDOITEM:
	    document = (AbstractDocument) text.getDocument();
	    undoableEditListeners = document.getUndoableEditListeners();
	    if (undoableEditListeners.length > 0) {
		UndoManager undo = (UndoManager) undoableEditListeners[0];
		undo.redo();
	    }
	    break;
	case UNDOITEM:

	    document = (AbstractDocument) text.getDocument();
	    undoableEditListeners = document.getUndoableEditListeners();
	    if (undoableEditListeners.length > 0) {
		UndoManager undo = (UndoManager) undoableEditListeners[0];
		undo.undo();
	    }
	    break;
	default:
	    break;
	}
    }

    @Override
    public void setComponent(JComponent component) {
	this.text = (JTextComponent) component;
    }

}
